﻿using System;
using System.Collections.Generic;
using Evpo.ContactData.Common;
using System.Data.SqlClient;
using System.Data;
using System.Linq;

namespace Evpo.ContactData.Data
{
    public class ContactInterestAdapter : AdapterBase, IEditableDictionaryItemAdapter
    {
        public ContactInterestAdapter(Connection connection) : base(connection) 
        {
            _internalAdapter = new ContactInterestAdapterInternal(connection);
        }

        #region Fields
        private readonly ContactInterestAdapterInternal _internalAdapter;
        #endregion

        #region Properties
        protected override string PrimaryKeyField
        {
            get { return Fields.ID; }
        }

        protected override string SProcToDelete
        {
            get { return SProcs.DeleteContactInterest; }
        }

        protected override string SProcToLoad
        {
            get { throw new NotImplementedException(); }
        }

        protected override string SProcToSave
        {
            get { return SProcs.SaveContactInterest; }
        }

        protected override string SProcToQuery
        {
            get { throw new NotImplementedException(); }
        }
        #endregion

        #region Methods

        public void SaveContactInterestsOfContact(int contactID, List<int> contactInterestIDs)
        {
            List<SqlParameter> parameters = new List<SqlParameter>()
            {
                new SqlParameter("@ContactID", contactID)
            };

            using (StatementExecutionProvider provider = 
                new StatementExecutionProvider(_conn, SProcs.ClearContactInterestsOfContact, parameters))
            {
                provider.ExecuteNonQuery();
            }

            
            SqlParameter contactIDPar = new SqlParameter("@ContactID", contactID);
            SqlParameter contactInterestIDPar = new SqlParameter("@ContactInterestID", SqlDbType.Int);
            parameters = new List<SqlParameter>()
            {
                contactIDPar,
                contactInterestIDPar
            };

            using (StatementExecutionProvider provider =
                new StatementExecutionProvider(_conn, SProcs.AddContactInterestOfContact, parameters))
            {
                foreach (int interestID in contactInterestIDs)
                {
                    contactInterestIDPar.Value = interestID;
                    provider.ExecuteNonQuery();
                }
            }
        }

        public List<ContactInterest> LoadContactInterestsOfContact(int contactID)
        {
            List<ContactInterest> retVal = new List<ContactInterest>();
            List<SqlParameter> parameters = new List<SqlParameter>()
            {
                new SqlParameter("@ContactID", contactID)
            };

            FillFromQuery<ContactInterest>(parameters, retVal, SProcs.LoadContactInterestsOfContact);

            return retVal;
        }

        public string GetDisplayName()
        {
            return _internalAdapter.GetDisplayName();
        }
        public DictionaryItem CreateNewItem()
        {
            return new ContactInterest()
            {
                InterestDescription = String.Empty
            };
        }

        public List<ContactInterest> GetItems()
        {
            return _internalAdapter.GetDictionaryItems<ContactInterest>(SProcs.GetContactInterests);
        }

        public ContactInterest GetItem(int id)
        {
            return _internalAdapter.GetDictionaryItem<ContactInterest>(id);
        }

        public void Save(ContactInterest item, bool isNew)
        {
            Save<ContactInterest>(item, isNew);
        }

        internal static void FillParametersToSave(ContactInterest contactInterest, List<SqlParameter> paramList)
        {
            paramList.Add(new SqlParameter("@" + Fields.InterestDescription, contactInterest.InterestDescription));
        }

        internal static void FillFieldsFromReader(ContactInterest contactInterest, ProcedureOutputReader procReader)
        {
            contactInterest.InterestDescription = procReader.GetString(Fields.InterestDescription);
        }

        public string GetName(int id)
        {
            return _internalAdapter.GetName(id);
        }

        public int GetIDForUnspecified()
        {
            return _internalAdapter.GetIDForUnspecified();
        }

        void IEditableDictionaryItemAdapter.Save(DictionaryItem item, bool isNew)
        {
            Save((ContactInterest)item, isNew);
        }

        List<DictionaryItem> IEditableDictionaryItemAdapter.GetItems()
        {
            return GetItems().Cast<DictionaryItem>().ToList();
        }

        DictionaryItem IEditableDictionaryItemAdapter.GetItem(int id)
        {
            return GetItem(id);
        }

        #endregion Methods

        #region Classes
        private class Fields
        {
            internal const string ID = "ID";
            internal const string Name = "Name";
            internal const string InterestDescription = "InterestDescription";
        }

        private class ContactInterestAdapterInternal : DictionaryItemAdapter
        {
            public ContactInterestAdapterInternal(Connection conn) : base(conn) { }
            protected override string GetItemSProc
            {
                get
                {
                    return SProcs.GetContactInterest;
                }
            }
            internal override void SetAdditionalParams(SqlDataReader sqlDataReader, DictionaryItem item)
            {
                base.SetAdditionalParams(sqlDataReader, item);
                ((ContactInterest)item).InterestDescription = sqlDataReader.GetString(sqlDataReader.GetOrdinal(Fields.InterestDescription));
            }

            public override string GetName(int id)
            {
                return GetDictionaryItem<ContactInterest>(id).Name;
            }

            protected override string DictionaryName
            {
                get
                {
                    return "ContactInterest";
                }
            }
        }
        #endregion Classes


    }
}
